local jiangnu = fk.CreateSkill {
  name = "th_jiangnu",
}

jiangnu:addEffect(fk.EventPhaseEnd, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return target.hp >= player.hp and player:hasSkill(jiangnu.name) and target.phase == target.Play
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local tos = {}
    room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
      local use = e.data
      if use.from == target then
        for _, p in ipairs(use.tos) do
          if p ~= player then
            table.insertIfNeed(tos, p)
          end
        end
      end
    end, Player.HistoryTurn)
    if #tos == 0 then return end
    local _, use = room:askToUseActiveSkill(player, {
      skill_name = "#th_jiangnu_viewas",
      skip = true,
      prompt = '#th_jiangnu-invoke:' .. target.id,
      extra_data = {
        skillName = jiangnu.name,
        exclusive_targets = table.map(tos, Util.IdMapper)
      }
    })
    if use then
      event:setCostData(self, { tos = use.targets, cards = use.cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    local cards = dat.cards[1]
    room:moveCardTo(cards, Card.DrawPile, nil, fk.ReasonPut, jiangnu.name, nil, nil, player)
    local card = Fk:cloneCard("duel")
    card.skillName = jiangnu.name
    room:useCard{
      from = player,
      tos = dat.tos,
      card = card
    }
  end
})

Fk:loadTranslationTable{
  ["th_jiangnu"] = "降怒",
  [":th_jiangnu"] = "体力值不小于你的角色出牌阶段结束时，你可以将一张黑色牌置于牌堆顶，视为对其本阶段使用牌指定过的目标使用【决斗】。",

  ["#th_jiangnu-invoke"] = "降怒：你可以将一张黑色牌置于牌堆顶，视为对 %src 本阶段使用牌指定过的目标使用【决斗】",
}

return jiangnu